package com.miplus.ccrm.core.service.action;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.core.controller.vo.action.FiAmortAddVo;
import com.miplus.ccrm.core.controller.vo.action.FiAmortDetailVo;
import com.miplus.ccrm.core.controller.vo.action.FiAmortResVo;
import com.miplus.ccrm.core.controller.vo.action.FiReliefResVo;
import com.miplus.ccrm.core.pojo.FiAmort;
import com.miplus.ccrm.core.pojo.FiAmortDetail;
import com.miplus.ccrm.core.mapper.FiAmortDetailMapper;
import com.miplus.ccrm.core.pojo.FiRelief;
import com.miplus.ccrm.core.utils.EnumFiAmortDStatus;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【fi_amort_detail(协商分期明细表)】的数据库操作Service实现
* @createDate 2024-04-12 21:15:18
*/
@Service
public class FiAmortDetailServiceImpl extends MPJBaseServiceImpl<FiAmortDetailMapper, FiAmortDetail>
    implements FiAmortDetailService{

    @Override
    public List<FiAmortDetailVo> fiDetailList(Integer amortid)
    {
        return selectJoinList(
                FiAmortDetailVo.class,
                new MPJLambdaWrapper<FiAmortDetail>()
                        .selectAs("fun_getnames('FI.AMORTDETAIL.STATUS',t.status)", FiAmortDetailVo::getStatusdsc)
                        .selectAs("(select sum(am.repayment) from fi_amort_detail am where am.amortid=t.amortid and am.pernumber<=t.pernumber and t.repayment is not NULL)",FiAmortDetailVo::getRepaymentsum)
                        .selectAs("(fi.amount-(select sum(am.repayment) from fi_amort_detail am where am.amortid=t.amortid and am.pernumber<=t.pernumber and t.repayment is not NULL))",FiAmortDetailVo::getRemainpay)
                        .innerJoin(FiAmort.class,"fi",FiAmort::getApplynumber,FiAmortDetail::getAmortid)
                        .selectAll(FiAmortDetail.class)
                        .eq(FiAmortDetail::getAmortid,amortid)
                        .orderByAsc(FiAmortDetail::getPernumber)
        );
    }

    @Override
    public int doCannel(Integer amortid)
    {
        FiAmortDetail fiAmortDetail=new FiAmortDetail();
        fiAmortDetail.setStatus(EnumFiAmortDStatus.EndPay.value);
        fiAmortDetail.setUpdt(new Date());
        return baseMapper.update(fiAmortDetail,
                new MPJLambdaWrapper<FiAmortDetail>()
                        .eq(FiAmortDetail::getAmortid,amortid)
                        .eq(FiAmortDetail::getStatus,EnumFiAmortDStatus.WaitPay.value)
        );
    }

    @Override
    public int doAdd(Integer amortid, FiAmortAddVo paras)
    {
        int pos=1;
        for(FiAmortDetailVo fiAmortDetailVo:paras.getFiAmortDetailVoList())
        {
            FiAmortDetail fiAmortDetail=new FiAmortDetail();
            fiAmortDetail.setAmortid(amortid);
            fiAmortDetail.setStatus(EnumFiAmortDStatus.WaitPay.value);
            fiAmortDetail.setPernumber(pos);
            fiAmortDetail.setPaydate(fiAmortDetailVo.getPaydate());
            fiAmortDetail.setAmount(fiAmortDetailVo.getAmount());
            fiAmortDetail.setAmountsum(fiAmortDetailVo.getAmountsum());
            fiAmortDetail.setPrin(fiAmortDetailVo.getPrin());
            fiAmortDetail.setInterest(fiAmortDetailVo.getInterest());
            fiAmortDetail.setExpense(fiAmortDetailVo.getExpense());
            fiAmortDetail.setUpdt(new Date());

            baseMapper.insert(fiAmortDetail);

            pos++;
        }

        return paras.getFiAmortDetailVoList().size();
    }
}




